<template>
  <div class="assignment-detail">
    <div class="header">
      <h1>{{ assignment.title }}</h1>
      <p class="due-date">截止日期：{{ formattedDueDate }}</p>
    </div>
    <div class="content">
      <h2>作业内容</h2>
      <p>{{ assignment.content }}</p>
      <div v-if="assignment.additionalDetails" class="additional-details">
        <h3>其他详细信息</h3>
        <p>{{ assignment.additionalDetails }}</p>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import {BASE_URL} from "@/config";

export default {
  props: ['homework_id'],
  data() {
    return {
      assignment:{},
      assignmentId: this.homework_id || '默认ID',
      assignmentTitle: this.title || '默认作业标题',
      assignmentContent: this.content || '默认作业内容',
    };
  },
  created() {
    console.log('接收到的 props 参数:', {
      homework_id: this.homework_id,
    });
    const id = this.$route.params.homework_id;
    this.fetchAssignmentDetails(id);
  },
  computed: {
    formattedDueDate() {
      return new Date(this.assignment.due_date).toLocaleString();
    }
  },
  methods: {
    async fetchAssignmentDetails(id) {
      try {
        // 发送带有作业 ID 的 POST 请求
        const response = await axios.post(`${BASE_URL}/homework/showHomework`, { homework_id: id });
        if (response.data && response.data.code === 200) {
          this.assignment = response.data.data || {}; // 根据实际 API 结构调整
          console.log('作业详情:', this.assignment);
        } else {
          console.error('获取作业详情失败:', response.data.message);
        }
      } catch (error) {
        console.error('获取作业详情请求失败:', error);
      }
    }
  },
};
</script>

<style scoped>
.assignment-detail {
  max-width: 800px;
  margin: 20px auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 8px;
  background-color: #f9f9f9;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.header {
  border-bottom: 2px solid #007bff;
  margin-bottom: 20px;
}

h1 {
  font-size: 24px;
  color: #333;
}

.due-date {
  color: #666;
  font-size: 14px;
}

.content {
  margin-top: 20px;
}

h2 {
  font-size: 20px;
  color: #007bff;
}

.additional-details {
  margin-top: 10px;
  padding: 10px;
  background-color: #e9ecef;
  border-radius: 5px;
}
</style>
